<?php
/**
 * oscshop2 B2C电子商务系统
 *
 *
 * @author    
 *
 */
namespace osc\admin\controller;

use osc\common\controller\AdminBase;
use think\Db;
use think\Request;

class Variety extends AdminBase{
	
	protected function _initialize(){
		parent::_initialize();
		$this->assign('breadcrumb1','交易品种管理');
		$this->assign('breadcrumb2','交易品种管理');
		$this->assign('breadcrumb6','品种价格');
	}
	
    public function index(){
    	
		//$pid=input('param.pid');
		
		//if(!$pid){
		//	$pid=0;
		//}
		
		$list = Db::name('exchage_variety')->where('state',1)->paginate(config('page_num'));
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
		$this->assign('list', $list);
		$this->assign('exchange', $exchange);
		
		return $this->fetch();   
    }
	
	public function add(){
		
		if(request()->isPost()){
			
			//$model=osc_model('admin','ArticleCategory');
			
			//$resault=$model->add(input('post.'));
			$data = input('post.');

			$ret = Db::name('exchage_variety')->where('name',$data['name'])->find();
			/*
			if($ret)
			{
				return ['error'=>'新增失败,交易已经存在'];	
			}
			*/
			if($data['name']=="" || $data['name'] ==null)
			{
				return ['error'=>'品种名字不能为空！'];
			}
			if($data['variety_id']=="" || $data['variety_id'] ==null)
			{
				return ['error'=>'品种编号不能为空！'];
			}
			$dt['name'] = $data['name'];
			$dt['remark'] = $data['content'];
			$dt['variety_id'] = $data['variety_id'];
			$dt['exchange_id'] = $data['cid'];
			$ret = Db::name('exchage_variety')->insert($dt);

			
											
			storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增交易品种成功');						
			return ['success'=>'新增成功','action'=>'add'];				
			
			
		}
		$this->assign('category',osc_system()->get_article_category_tree());
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$this->assign('d',Db::name('exchage_variety')->where('state',1)->find((int)input('param.id')));
		$this->assign('action',url('variety/add'));
		$this->assign('exchange',$exchange);
		$this->assign('crumbs','新增');
		return $this->fetch('edit');
	}
	
	public function edit(){
		
		if(request()->isPost()){
			
			//$model=osc_model('admin','ArticleCategory');
			
			//$resault=$model->add(input('post.'));
			$data = input('post.');

			$ret = Db::name('exchage_variety')->where('name',$data['name'])->find();
			/*
			if($ret)
			{
				return ['error'=>'新增失败,交易已经存在'];	
			}
			*/
			if($data['name']=="" || $data['name'] ==null)
			{
				return ['error'=>'品种名字不能为空！'];
			}
			if($data['variety_id']=="" || $data['variety_id'] ==null)
			{
				return ['error'=>'品种编号不能为空！'];
			}
			$dt['name'] = $data['name'];
			$dt['remark'] = $data['content'];
			$dt['variety_id'] = $data['variety_id'];
			$dt['exchange_id'] = $data['cid'];
			$ret = Db::name('exchage_variety')->where('id',$data['id'])->update($dt);

			
											
			storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改交易品种成功');						
			return ['success'=>'修改成功','action'=>'add'];				
			
			
		}
		$this->assign('category',osc_system()->get_article_category_tree());
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$this->assign('d',Db::name('exchage_variety')->where('state',1)->find((int)input('param.id')));
		$this->assign('action',url('variety/edit'));
		$this->assign('exchange',$exchange);
		$this->assign('crumbs','修改');
		return $this->fetch('edit');
	}
	
	

	//更新排序
	function update_sort(){
		$data=input('post.');
		
		$update['id']=(int)$data['cid'];
		$update['sort_order']=(int)$data['sort'];
		
		if(Db::name('ArticleCategory')->update($update)){
			
			storage_user_action(UID,admin('username'),config('BACKEND_USER'),'更新了文章分类排序');
			
			return true;
		}		
	}


	/**
	 * 修改交易所品种正在交易的状态
	 */
	function use_state(){
		$data = $this->request->param();
		if($data['status'] == 0){
			Db::name('exchage_variety')->where('use_state',1)->update(['use_state'=>0]);
			Db::name('exchage_variety')->update(['id'=>$data['id'],'use_state'=>1]);
		}else{
			Db::name('exchage_variety')->update(['id'=>$data['id'],'use_state'=>0]);
		}
		$this->redirect('Variety/index');
	}

	/**
	 * 节假日管理
	 * @return mixed
	 */
	public function holiday(){
		$list = Db::name('holiday')->order('date_dt desc')->paginate(config('page_num'));
		$this->assign('breadcrumb1','节假日管理');
		$this->assign('breadcrumb2','节假日列表');
		$this->assign('list',$list);
		$this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
		return $this->fetch();
	}

	/**
	 * 新增节假日期
	 */
	public function add_holiday(){
		$data = $this->request->param();
		if($data){
			$map = [
				'unix_dt'	=> strtotime($data['holiday_time']),
				'date_dt'   => $data['holiday_time']
			];
			$info = Db::name('holiday')->where(['unix_dt'=>$map['unix_dt']])->count();
			if(!$info){
				$res = Db::name('holiday')->insert($map);
			}
		}
		$this->redirect('variety/holiday');
	}

	/**
	 * 删除节假日期
	 */
	public function delete_holiday(){
		$data = $this->request->param();
		if($data){
			$res = Db::name('holiday')->where('id',$data['id'])->delete();
		}
		$this->redirect('variety/holiday');
	}


	/**
	 * 品种价格配置
	 */
	public function issue_config(){
		$param = $this->request->param();
		$map = [];
		if(!empty($param['exchange'])){
			$map['b.id'] = $param['exchange'];
		}
		if(!empty($param['variety'])){
			$map['c.id'] = $param['variety'];
		}
		$list = Db::name('contract_issue a,osc_exchange_house b,osc_exchage_variety c')
				->where($map)
				->where('a.exchange_id=b.id and a.variety_id=c.id')
				->field('a.id,a.issue,a.target,a.remark,b.name exchange,c.name variety')
				->paginate(10,false, [
						'query' => Request::instance()->param(),//不丢失已存在的url参数
				]);
		$page = $list->render();
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$variety = Db::name('exchage_variety')->where('state',1)->select();
		$this->assign('list',$list);
		$this->assign('page',$page);
		$this->assign('exchange',$exchange);
		$this->assign('variety',$variety);
		return $this->fetch();
	}


	/**
	 * 新增品种价格
	 * @return mixed
	 */
	public function issue_add(){
		if(request()->isPost()){
			$data = $this->request->post();
			$res = Db::name('contract_issue')->insertGetId($data);
			if($res){
				storage_user_action(UID,admin('username'),config('BACKEND_USER'),'新增品种价格--'.$res);
				$this->success('新增成功！',url('Contract/issue_config'));
			}else{
				$this->error('新增失败！');
			}
		}
		$exchange = Db::name('exchange_house')->where('state',1)->select();
		$variety = Db::name('exchage_variety')->where('state',1)->select();
		$this->assign('exchange',$exchange);
		$this->assign('variety',$variety);
		$this->assign('crumbs', '新增');
		return $this->fetch();
	}

	/**
	 * 修改品种价格
	 * @return mixed
	 */
	public function issue_update(){
		if(request()->isPost()){
			$data = $this->request->post();
			$res = Db::name('contract_issue')->where('id',$data['id'])->update($data);
			if($res){
				storage_user_action(UID,admin('username'),config('BACKEND_USER'),'修改了品种价格--'.$data['id']);
				$this->success('修改成功！',url('Contract/issue_config'));
			}else{
				$this->error('修改失败！');
			}
		}else{
			$data = Db::name('contract_issue')->where('id',input('id'))->find();
			$exchange = Db::name('exchange_house')->where('state',1)->select();
			$variety = Db::name('exchage_variety')->where('state',1)->select();
			$this->assign('exchange',$exchange);
			$this->assign('variety',$variety);
			$this->assign('info',$data);
		}
		$this->assign('crumbs', '修改');
		return $this->fetch();
	}
}
